App({
    globalData: {
      // prefixUrl:'http://localhost:8085',//'http://localhost:8085'
      prefixUrl:'https://www.sugarttt.top',
      user: null,
      openId:'',
      userId: 0,
      token:'',
    },
    onLaunch() {
      var that = this
      let userInfo = wx.getStorageSync('userInfo')
      
      if (userInfo != null && userInfo != '') {
        that.globalData.userId = userInfo.id
        that.globalData.user = userInfo
        that.globalData.openId = userInfo.openId
        console.log("storageUerInfo: ", userInfo)
      } else {
      wx.login({
        success (res) {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          var code=res.code;
          if(code){
            wx.request({
              // openId
              url: that.globalData.prefixUrl+'/chong/wx/login?code='+code,
              success(e){
                that.globalData.openId=e.data.openid
                console.log(that.globalData.openId)
                // 根据openid查找user
                wx.request({
                  url: that.globalData.prefixUrl+"/chong/wx/"+that.globalData.openId,
                  success(e){
                    console.log("app.js onLoad查询的用户信息：", e)
                    that.globalData.user=e.data
                    // that.globalData.token=e.header.Authorization
                    // user存在
                    if (that.globalData.user != null) {
                      console.log("databaseUerInfo: ", e.data)
                      that.globalData.userId=e.data.id
                      wx.setStorageSync('userInfo', that.globalData.user)
                    }
                    console.log('测试',that.globalData.token)
                    if(e.data.code==601){
                      wx.showModal({
                        title: '提示',
                        content: '您还未注册，无法使用所有功，点击确定前往注册',
                        success (res) {
                          if (res.confirm) {
                            console.log('用户点击确定')
                            // wx.navigateTo({
                            //   url: '/pages/register/register',
                            // })
                          } else if (res.cancel) {
                            console.log('用户点击取消')
                          }
                        }
                      })
                    }
                  }
                })
              },fail(e){
                wx.showToast({
                  icon: 'error',
                  title: '网络连接失败',
                })
              }
            })
          }
        }
      })
    }
    },

    // 这个login无用
    login(e){
      var that=this
      return new Promise(function(resolve, reject) {
        wx.login({
          success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            var code=res.code;
            if(code){
              wx.request({
                // openId
                url: that.globalData.prefixUrl+'/chong/wx/login?code='+code,
                success(e){
                  that.globalData.openId = e.data.openid
                  console.log(e)
                  console.log(e.data.openid)
                  // 根据openid查找user
                  wx.request({
                    url: that.globalData.prefixUrl+"/chong/wx/"+that.globalData.openId,
                    success(e){
                      console.log("user信息: ",e.data)
                      that.globalData.user = e.data
                      // that.globalData.token=e.header.Authorization
                      // user存在
                      if (e.data.data != null) {
                        that.globalData.userId=that.globalData.user.id
                        wx.setStorageSync('userInfo', that.globalData.user)
                      }
                      console.log('测试token',that.globalData.token)
                      if(e.data.code==601){
                        wx.showModal({
                          title: '提示',
                          content: '您还未注册，无法使用所有功，点击确定前往注册',
                          success (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                              wx.navigateTo({
                                url: '/pages/register/register',
                              })
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      }
                      // if(that.globalData.user.isValid==false){
                      //   for(var i=0;;i++){
                      //     wx.showModal({
                      //       title: '提示',
                      //       content: '您不能使用此小程序，请联系管理员',
                      //       success (res) {
                      //       }
                      //     })
                      //   }
                      // }
                      resolve()
                    }
                  })
                },fail(e){
                  wx.showToast({
                    icon: 'error',
                    title: '网络连接失败',
                  })
                }
              })
            }
          }
        })
      })
    }
  })
  
  